<template>
  <!-- 通用查看签到弹框 -->
  <base-modal
    :footer="null"
    title="签到人员"
    :closable="true"
    @cancel="handleClose"
    :visible="visible"
    width="1200px"
  >
    <s-table
      bordered
      ref="table"
      size="default"
      rowKey="key"
      :columns="column"
      :data="loadData"
      :alert="true"
      show-size-changer
      show-quick-j
      umper
      :showPagination="true"
    >
      <span
        slot="serial"
        slot-scope="text, record, index"
      >{{ index + 1 }}</span>
    </s-table>
  </base-modal>
</template>

<script>
import { meetingEmployee } from '@/api/office/conference'
import training from '@/api/design/training'
import BaseModal from '@/components/BaseModal'
import { STable } from '@/components'

const column = [
  {
    title: '序号',
    dataIndex: 'serial',
    width: '8%',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '所属单位',
    dataIndex: 'orgName',
    width: '18%',
    ellipsis: true,
  },
  {
    title: '岗位',
    dataIndex: 'postName',
    width: '13%',
    ellipsis: true,
  },
  {
    title: '姓名',
    dataIndex: 'userName',
    width: '10%',
    ellipsis: true,
  },
  {
    title: '是否签到',
    dataIndex: 'sign',
    width: '9%'
  },
  {
    title: '签到地点',
    dataIndex: 'address',
    width: '27%',
    ellipsis: true,
  },
  {
    title: '签到时间',
    dataIndex: 'date',
    ellipsis: true,
  }
]

export default {
  components: {
    BaseModal,
    STable
  },
  props: {
    id: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    },
    isNew: {
      type: Boolean,
      default: false
    },
    api: {
      type: [Object, Function],
      default() {
        return null
      }
    }
  },
  data() {
    this.column = column
    return {
      loadData: async (parameter) => {
        if (this.isNew) {
          const requestParameters = Object.assign(
            {},
            {
              pageNum: parameter.pageNo,
              pageSize: parameter.pageSize
            },
            {
              param: { id: this.id }
            }
          )
          const {
            data: { list, pageNum, total },
            code
          } = await training.getEmployeeSignPage(requestParameters)
          if (code === 200) {
            list.forEach((item) => {
              item.orgName = item.orgName?.join('/')
              item.postName = item.postName?.join('/')
              let sign = null
                if(!item.orgid?.length && item.sign){
                  item.sign = '游客签到'
                }else if(item.signIn){
                  item.sign  = '是'
                }else{
                  item.sign  = '否'
                }
            })
            console.log(list,1111)
            return Promise.resolve({
              data: list,
              currentPage: pageNum,
              totalSize: total
            })
          } else {
            return Promise.reject()
          }
        } else {
          const requestParameters = Object.assign({}, parameter, { id: this.id })
          let [err, res] = [false, null]
          if (this.api) {
            let res2 = await this.api(requestParameters)
            if (res2.code === this.SUCCESS_CODE) {
              res = res2
            } else {
              err = true
            }
          } else {
            [err, res] = await meetingEmployee(requestParameters)
          }
          return new Promise((resolve, reject) => {
            if (err) {
              reject(err)
            } else {
              res.data.forEach((item) => {
                item.orgName = item.orgName?.join('/')
                item.postName = item.postName?.join('/');
                let sign = null
                if(!item.orgid?.length && item.sign){
                  item.sign = '游客签到'
                }else if(item.signIn){
                  item.sign = '是'
                }else{
                  item.sign = '否'
                }
              })
              console.log(res,2222)
              resolve(res)
            }
          })
        }
      }
    }
  },
  created() {
  },
  methods: {
    handleClose() {
      this.$emit('confirm')
    }
  },
  watch: {
    visible(v) {
      if (v) {
        this.$nextTick(() => {
          this.$refs.table.refresh()
        })
      } else {
        if (this.$refs.table) {
          this.$refs.table.localDataSource = []
        }
      }
    }
  }
}
</script>

